//
//  ZHFPhotoDetailViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFPhotoDetailViewController.h"

#import "ZHFPhotoHeadView.h"
#import "ZHFPhotoFootView.h"
#import "ZHFTNDetailCell.h"
#import "ZHFPhotoDetailCell.h"
#import "ZHFPhoto.h"

@interface ZHFPhotoDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) ZHFPhotoHeadView *headView;

@property (nonatomic, strong) ZHFPhotoFootView *footView;

@property (nonatomic, assign) BOOL isShowHeader;

@end

@implementation ZHFPhotoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //初始化
    self.index = self.row;
    self.isShowHeader = YES;
    
    //状态栏
    [UIApplication sharedApplication].statusBarHidden = YES;
    
    //collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = self.view.backgroundColor;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.bounces = NO;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ZHFPhotoDetailCell class] forCellWithReuseIdentifier:@"TNDetailItem"];
    [self.view addSubview:self.collectionView];
    
    //头
    self.headView = [[ZHFPhotoHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    [self.view addSubview:self.headView];
    
    //尾
    self.footView = [[ZHFPhotoFootView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)];
    [self.view addSubview:self.footView];
    
    
    self.headView.title = [[self.array objectAtIndex:self.index] trip_name];
    self.footView.photo = [self.array objectAtIndex:self.index];
    
    
    //返回按钮(叉号按钮)
    UIButton *returnBut = [UIButton buttonWithType:UIButtonTypeCustom];
    returnBut.frame = CGRectMake(20 * ADAPT_WIDTH, 10 * ADAPT_HEIGHT, 32 * ADAPT_WIDTH, 32 * ADAPT_HEIGHT);
    returnBut.tag = 1000;
    [returnBut setImage:[[UIImage imageNamed:@"iconfont-addcollapse.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    returnBut.tintColor = [UIColor whiteColor];
    [returnBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:returnBut];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    //状态栏
    [UIApplication sharedApplication].statusBarHidden = YES;
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.row inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
    self.index = self.row;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //头尾显示
    if (self.isShowHeader) {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.headView.frame = CGRectMake(0, -44, SCREEN_WIDTH, 44);
            self.footView.alpha = 0;
        }];
    }
    else {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
            self.footView.alpha = 1;
        }];
    }
    self.isShowHeader = !self.isShowHeader;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHFPhotoDetailCell *TNDetailCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TNDetailItem" forIndexPath:indexPath];
    
    ZHFPhoto *photo = [self.array objectAtIndex:indexPath.row];
    TNDetailCell.image_url = photo.image_url;
    
    return TNDetailCell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat f = scrollView.contentOffset.x / SCREEN_WIDTH;
    self.index = (NSInteger)f;
    
    self.headView.title = [[self.array objectAtIndex:self.index] trip_name];
    self.footView.photo = [self.array objectAtIndex:self.index];
    
    if (self.index > self.array.count - 3) {
        [self.delegate netwotking];
    }
    
}

#pragma mark array set方法
- (void)setArray:(NSMutableArray *)array {
    
    
    _array = array;
    
    [self.collectionView reloadData];
}

#pragma mark 返回
- (void)buttonAction:(UIButton *)button{
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    //返回当前浏览图片位置
    [self.delegate scrollCellWithIndex:self.index];
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
